#include "mainwindow.h"
#include "ui_mainwindow.h"

QUrl MainWindow::getUrlfromItem(QListWidgetItem *item)
{
    QVariant va=item->data(Qt::UserRole);
    QUrl ur=va.value<QUrl>();
    return ur;
}

void MainWindow::do_sourceChange(const QUrl &media)
{
    ui->label_director->setText(media.fileName());
    QPixmap pm(hash_picture[media.fileName()]);
    pm=pm.scaled(ui->label_picture->width(),ui->label_picture->height());
    ui->label_picture->setPixmap(pm);

}

void MainWindow::do_durationChange(qint64 duration)
{
    my_duration=duration;
    my_position=0;
    ui->horizontalSlider_2->setMaximum(my_duration);
    int secs=my_duration/1000;
    int minu=secs/60;
    secs=secs%60;
    QString Sec;
    if(secs<10)
        Sec="0"+QString::number(secs);
    else
        Sec=QString::number(secs);
    ui->label_time->setText(QString::asprintf("0:00/%d:",minu)+Sec);

}

void MainWindow::do_positionChange(qint64 positon)
{
    if(ui->horizontalSlider_2->isSliderDown())
        return;
    my_position=positon;
    ui->horizontalSlider_2->setValue( positon);
    int secs= positon/1000;
    int minu=secs/60;
    secs=secs%60;
    QString Sec1;
    if(secs<10)
        Sec1="0"+QString::number(secs);
    else
        Sec1=QString::number(secs);
    QString Sec;
    int secs1=my_duration/1000;
    int minu1=secs1/60;
    secs1=secs1%60;
    if(secs1<10)
        Sec="0"+QString::number(secs1);
    else
        Sec=QString::number(secs1);
    ui->label_time->setText(QString::number(minu)+":"+Sec1+"/"+QString::number(minu1)+":"+Sec);
}

void MainWindow::do_stateChange(QMediaPlayer::PlaybackState state)
{
    if(manual_set)
    {
        manual_set=false;
        return;
    }
    if(state==QMediaPlayer::StoppedState)
    {
        if(loop)     //循环播放的情况
        mp->play();
        else           //不循环自动播放下一首音乐
        {
            int row=ui->listWidget->currentRow();
            row++;
            int count=ui->listWidget->count();
            if(row>=count)
                row=0;
            ui->listWidget->setCurrentRow(row);
            mp->setSource(getUrlfromItem(ui->listWidget->currentItem()));
            mp->play();
        }

    }
}

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    mp=new QMediaPlayer(this);
    QAudioOutput* ao=new QAudioOutput(this);
    mp->setAudioOutput(ao);
    QPixmap pm(":/widget.png");
    pm=pm.scaled(ui->label_picture->width(),ui->label_picture->height());
    ui->label_picture->setPixmap(pm);
    connect(mp,&QMediaPlayer::sourceChanged,this,&MainWindow::do_sourceChange);
    connect(mp,&QMediaPlayer::durationChanged,this,&MainWindow::do_durationChange);
    connect(mp,&QMediaPlayer::positionChanged,this,&MainWindow::do_positionChange);
     connect(mp,&QMediaPlayer::playbackStateChanged,this,&MainWindow::do_stateChange);
    hash_picture["1.mp3"]=":/lantingxu.png";
    hash_picture["2.mp3"]=":/zhishaohaiyouni.png";
    hash_picture["3.mp3"]=":/turanhaoxiangni.png";
    hash_picture["玉剑风流.mp3"]=":/yujianfengliu.png";


}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_start_clicked()
{
    if(ui->listWidget->count()==0)   //没有音乐播放时
        return;
    if(mp->mediaStatus()==QMediaPlayer::StoppedState)
    {
        mp->setSource(getUrlfromItem(ui->listWidget->currentItem()));
        mp->play();
    }
    else
    {
        mp->setPosition(my_position);
        mp->play();
    }
    ui->pushButton_start->setEnabled(false);
    ui->pushButton_pause->setEnabled(true);

}


void MainWindow::on_pushButton_pause_clicked()
{
    mp->pause();
    ui->pushButton_pause->setEnabled(false);
    ui->pushButton_start->setEnabled(true);

}


void MainWindow::on_pushButton_initial_clicked()
{
    my_position=0;
    mp->stop();
    ui->pushButton_start->setEnabled(true);
    ui->pushButton_pause->setEnabled(false);
}


void MainWindow::on_pushButton_front_clicked()
{
    manual_set=true;
    int row=ui->listWidget->currentRow();
    row--;
    if(row<0)
    {
        row=ui->listWidget->count()-1;

    }
    ui->listWidget->setCurrentRow(row);
    my_position=0;
    mp->setSource(getUrlfromItem(ui->listWidget->currentItem()));
    mp->play();
}


void MainWindow::on_pushButton_next_clicked()
{
     manual_set=true;
    int row=ui->listWidget->currentRow();
    row++;
    int cound=ui->listWidget->count();
    if(row>=cound)
    {
        row=0;
    }
    ui->listWidget->setCurrentRow(row);
    my_position=0;
    mp->setSource(getUrlfromItem(ui->listWidget->currentItem()));
    mp->play();
}


void MainWindow::on_pushButton_add_clicked()
{
    QString current_path=QDir::currentPath();
    QStringList StrList=QFileDialog::getOpenFileNames(this,"选择你的音乐",current_path,"");
    for(int i=0;i<StrList.size();i++)
    {
        QString aFile=StrList.at(i);
        QFileInfo FI(aFile);        //获取文件的属性
        QListWidgetItem* item=new QListWidgetItem(FI.fileName());
        item->setData(Qt::UserRole,QUrl::fromLocalFile(aFile));
        item->setIcon(QIcon(":/music.png"));
        ui->listWidget->addItem(item);
    }
    ui->listWidget->setCurrentRow(0);   //自动设置为第一行
}


void MainWindow::on_pushButton_reduce_clicked()
{
    int row=ui->listWidget->currentRow();
    ui->listWidget->takeItem(row);
}


void MainWindow::on_pushButton_clear_clicked()
{
    ui->listWidget->clear();
}


void MainWindow::on_comboBox_currentIndexChanged(int index)
{
    QString str=ui->comboBox->currentText();
    QString S;
    for(int i=0;i<3;i++)
        S+=str[i];
    float a=S.toFloat();
    mp->setPlaybackRate(a);
}


void MainWindow::on_listWidget_itemDoubleClicked(QListWidgetItem *item)
{
    mp->setSource(getUrlfromItem(ui->listWidget->currentItem()));
    mp->play();
}


void MainWindow::on_horizontalSlider_valueChanged(int value)
{
    mp->audioOutput()->setVolume(value/100.0);
}




void MainWindow::on_horizontalSlider_2_sliderMoved(int position)
{
    mp->setPosition(position);
}


void MainWindow::on_pushButton_9_clicked()
{
    if(loop==false)
    {
        loop=true;
        mp->setLoops(-1);
        ui->pushButton_9->setIcon(QIcon(":/circular-1.png"));

    }
    else
    {
        loop=false;
        mp->setLoops(2);
        ui->pushButton_9->setIcon(QIcon(":/circular.png"));
    }
}

